package com.winderinfo.yikaotianxia.ui.study;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.winderinfo.yikaotianxia.R;


import java.util.List;

public class StudyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<StudyBean.CoursesBean> list;

    public StudyAdapter(Context context, List<StudyBean.CoursesBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_study, viewGroup, false);
        SelectedViewHolder selectedViewHolder = new SelectedViewHolder(view);
        return selectedViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        SelectedViewHolder selectedViewHolder = (SelectedViewHolder) viewHolder;
        selectedViewHolder.st_title.setText(list.get(i).getTitle());
        selectedViewHolder.st_time.setText("学习时间: " + list.get(i).getXuexitime() + "分钟");
        selectedViewHolder.st_sum.setText("学习进度" + list.get(i).getYikannum());
        Glide.with(context)
                .load(list.get(i).getPhoto())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))//圆角半径
                .into(selectedViewHolder.st_image);
        //回调ID
        selectedViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = list.get(i).getId();
                String details = list.get(i).getDetails();
                String photo = list.get(i).getPhoto();
                if (onGoodsId != null) {
                    onGoodsId.getId(id, details, photo);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class SelectedViewHolder extends RecyclerView.ViewHolder {

        private final TextView st_time, st_title, st_sum;
        private final ImageView st_image;

        public SelectedViewHolder(@NonNull View itemView) {
            super(itemView);
            st_image = itemView.findViewById(R.id.st_image);
            st_time = itemView.findViewById(R.id.st_time);
            st_title = itemView.findViewById(R.id.st_title);
            st_sum = itemView.findViewById(R.id.st_sum);
        }
    }

    //接口回调
    public interface OnGoodsId {
        void getId(int id, String details, String photo);
    }

    public OnGoodsId onGoodsId;

    public void setOnGoodsId(OnGoodsId onGoodsId) {
        this.onGoodsId = onGoodsId;
    }

}
